﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class SplitAndMerge
{
    public const string FILENAME = "Test.mp3";
    public const int FIVE_KB = 5 * 1024;

    public static void SplitFile(string fileName)
    {
        using (FileStream readStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            string newFilePart1 = string.Format("{0}-part_1.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
            string newFilePart2 = string.Format("{0}-part_2.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);

            using (FileStream writeStream1 = new FileStream(newFilePart1, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (FileStream writeStream2 = new FileStream(newFilePart2, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    bool useFirstFile = true;
                    while (true)
                    {
                        byte[] buffer = new byte[FIVE_KB];
                        int bytesRead = readStream.Read(buffer, 0, FIVE_KB);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        // changes the file to write to
                        if (useFirstFile)
                        {
                            writeStream1.Write(buffer, 0, bytesRead);
                        }
                        else
                        {
                            writeStream2.Write(buffer, 0, bytesRead);                            
                        }
                        useFirstFile = !useFirstFile;
                    }
                }
            }
        }
    }

    public static void MergeFilesToOriginal(string fileName)
    {
        string newFilePart1 = string.Format("{0}-part_1.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
        string newFilePart2 = string.Format("{0}-part_2.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
        string[] nameParts = fileName.Split('.');
        string outputFilename = string.Format("{0}-original.{1}", nameParts[0], nameParts[1]);
        using (FileStream readStream1 = new FileStream(newFilePart1, FileMode.Open, FileAccess.Read))
        {
            using (FileStream readStream2 = new FileStream(newFilePart2, FileMode.Open, FileAccess.Read))
            {
                using (FileStream wrtieStream = new FileStream(outputFilename, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    bool useFirstFile = true;
                    while (true)
                    {
                        byte[] buffer = new byte[FIVE_KB];
                        int bytesRead;
                        if (useFirstFile)
                        {
                            bytesRead = readStream1.Read(buffer, 0, FIVE_KB);
                        }
                        else
                        {
                            bytesRead = readStream2.Read(buffer, 0, FIVE_KB);
                        }

                        if (bytesRead == 0)
                        {
                            break;
                        }
                        wrtieStream.Write(buffer, 0, bytesRead);                     
                        useFirstFile = !useFirstFile;
                    }
                }
            }
        }
    }
}

